﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace MyDemos.Utils
{
    public class Big5Util
    {

        private static Dictionary<int, int> mBIG5_Unicode_MAP = new Dictionary<int, int> ();

        static Big5Util ()
        {
            StreamResourceInfo _ResourceInfo = Application.GetResourceStream (new Uri ("MyDemos;component/Resource/BIG5.TXT", UriKind.RelativeOrAbsolute));

            StreamReader _Reader = new StreamReader (_ResourceInfo.Stream);


            String line;

            while ((line = _Reader.ReadLine ()) != null)
            {
                if (line.StartsWith ("#"))
                    continue; // Comments 
                string[] lTokens = line.Split (new char[] { '\t' });
                if (lTokens.Length < 2)
                    continue; // Not enough tokens 
                try
                {
                    mBIG5_Unicode_MAP.Add (int.Parse (lTokens[0].Substring (2), NumberStyles.HexNumber), int.Parse (lTokens[1].Substring (2), NumberStyles.HexNumber));
                }
                catch (Exception)
                {
                    continue; // No mapping 
                }
            }
        }

        public static string ToUni (Stream _str)
        {
            StringBuilder _StringBuilder = new StringBuilder ();
            byte[] big5Buffer = new byte[2];

            int input;
            while ((input = _str.ReadByte ()) != -1)
            {

                if (input > 0x81 && big5Buffer[0] == 0) //lead byte of big 5 
                {
                    big5Buffer[0] = (byte)input;
                }
                else if (big5Buffer[0] != 0)
                {
                    big5Buffer[1] = (byte)input;
                    int Big5Char = (big5Buffer[0] << 8) + big5Buffer[1];
                    try
                    {
                        int UTF8Char = mBIG5_Unicode_MAP[Big5Char];
                        _StringBuilder.Append ((char)UTF8Char);
                    }
                    catch (Exception)
                    {

                        _StringBuilder.Append ((char)mBIG5_Unicode_MAP[0xA148]); // No mapping, use replacement character 
                    }

                    big5Buffer = new byte[2];
                }
                else
                {
                    _StringBuilder.Append ((char)input); // ASCII character 
                }
            }
            return _StringBuilder.ToString ();
        }
    }
}
